package com.hpjz.emrecord.web.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import lombok.extern.log4j.Log4j;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hpjz.common.ParameterBean;
import com.hpjz.common.ResultBean;
import com.hpjz.common.data.PageData;
import com.hpjz.common.utils.ConstantDefinition;
import com.hpjz.emrecord.service.IEmRecordService;
import com.hpjz.emrecord.web.vo.EmRecordVO;
import com.hpjz.emrecord.web.vo.PatientInfoVO;

/**
 *  电子病历控制接口
 *  
 * @author bbchen
 *
 */
@Controller
@RequestMapping("/emRecord")
@Log4j
public class EmRecordController {
	
	@Resource
	private IEmRecordService emRecordService;
	
	@RequestMapping(value = "/insertEmRecord",method = RequestMethod.POST)
	public @ResponseBody ResultBean insertEmRecord(@RequestBody ParameterBean pb){
		
		PatientInfoVO objMap = (PatientInfoVO) pb.getParameter(PatientInfoVO.class);
		ResultBean result = new ResultBean();
		 try {
			 Map<String,Long> data =  this.emRecordService.insertEmRecord(objMap);
			 result.setResult(data);
			 result.setStatus(ConstantDefinition.SYSTEM_SUCCESS);
		} catch (Exception e) {
			 log.error("添加电子病历失败！",e);
			 result.setStatus(ConstantDefinition.SYSTEM_ERROR);
		}
		 return result;
	}
	
	
	/**
	 * 根据条件查询电子病历(中)
	 * @param pb
	 * @return
	 */
	@RequestMapping(value ={"/findEmRecords"},method = RequestMethod.POST)
	public @ResponseBody ResultBean findEmRecords(@RequestBody ParameterBean pb){
		EmRecordVO objMap = (EmRecordVO) pb.getParameter(EmRecordVO.class);
		ResultBean result = new ResultBean();
		 try {
			 List<EmRecordVO> evos = this.emRecordService.findEmRecords(objMap);
			 result.setResult(evos);
			 result.setStatus(ConstantDefinition.SYSTEM_SUCCESS);
		} catch (Exception e) {
			 log.error("查询电子病历失败！",e);
			 result.setStatus(ConstantDefinition.SYSTEM_ERROR);
		}
		 return result;
	}
	
	 /**
	  * 修改电子病历信息
	  * @param pb
	  * @return
	  */
	@RequestMapping(value = "/updateEmRecord",method = RequestMethod.POST)
	public @ResponseBody ResultBean updateEmRecord(@RequestBody ParameterBean pb){
		PatientInfoVO objMap = (PatientInfoVO) pb.getParameter(PatientInfoVO.class);
		ResultBean result = new ResultBean();
		 try {
			 this.emRecordService.updateEmRecord(objMap);
			 result.setStatus(ConstantDefinition.SYSTEM_SUCCESS);
		} catch (Exception e) {
			 log.error("修改电子病历失败！",e);
			 result.setStatus(ConstantDefinition.SYSTEM_ERROR);
		}
		 return result;
	}
	
	
	/**
	 * 删除电子病历
	 * @param pb
	 * @return
	 */
	@RequestMapping(value = "/deleteEmRecord",method = RequestMethod.POST)
	public @ResponseBody ResultBean deleteEmRecord(@RequestBody ParameterBean pb){
		EmRecordVO objMap = (EmRecordVO) pb.getParameter(EmRecordVO.class);
		ResultBean result = new ResultBean();
		 try {
			 this.emRecordService.deleteEmRecord(objMap);
			 result.setStatus(ConstantDefinition.SYSTEM_SUCCESS);
		} catch (Exception e) {
			 log.error("删除电子病历失败！",e);
			 result.setStatus(ConstantDefinition.SYSTEM_ERROR);
		}
		 return result;
	}
	
	/**
	 * 查询单条电子病历
	 * @param pb
	 * @return
	 */
	@RequestMapping(value = "/findEmRecord",method = RequestMethod.POST)
	public @ResponseBody ResultBean findEmRecord(@RequestBody ParameterBean pb){
		EmRecordVO objMap = (EmRecordVO) pb.getParameter(EmRecordVO.class);
		ResultBean result = new ResultBean();
		 try {
			 objMap.setStatus("1");
			 EmRecordVO vo =  this.emRecordService.findEmRecord(objMap);
			 result.setResult(vo);
			 result.setStatus(ConstantDefinition.SYSTEM_SUCCESS);
		} catch (Exception e) {
			 log.error("查询电子病历失败！",e);
			 result.setStatus(ConstantDefinition.SYSTEM_ERROR);
		}
		 return result;
	}
	
	
	@RequestMapping(value = "/updateEmRecordStatus",method = RequestMethod.POST)
	public @ResponseBody ResultBean updateEmRecordStatus(@RequestBody ParameterBean pb){
		Map objMap = (Map) pb.getParameter(Map.class);
		ResultBean result = new ResultBean();
		 try {
			 String recordid = (String) objMap.get("recordid");
			 this.emRecordService.updateEmRecordStatus(recordid);
			 result.setStatus(ConstantDefinition.SYSTEM_SUCCESS);
		} catch (Exception e) {
			 log.error("修改电子病历状态失败！",e);
			 result.setStatus(ConstantDefinition.SYSTEM_ERROR);
		}
		 return result;
	}
	
	
	@RequestMapping(value = "/findEmcordsByPage",method = RequestMethod.POST)
	public @ResponseBody ResultBean findEmcordsByPage(@RequestBody ParameterBean pb){
		Map objMap = (Map) pb.getParameter(Map.class);
		objMap.put("uid",pb.getUid());
		ResultBean result = new ResultBean();
		
		 try {
			//判断如果是android端不让登录
			 String platform =  pb.getPlatform();
			 if(ConstantDefinition.ANDROID_PLATFORM.equalsIgnoreCase(platform)) {
			     result.setStatus(ConstantDefinition.USER_FORBID);
			     result.setResult("用户登录不合法！");
			     return  result;
			 }
			 objMap.put("platform", platform);
			 Map data = this.emRecordService.findEmRecordsPage(objMap);
			 if (data.get("error")!=null) {
				result.setStatus(ConstantDefinition.USER_FORBID);
				result.setResult("用户登录不合法！");
				return result;
			 }
			 result.setResult(data.get("data"));
			 result.setStatus(ConstantDefinition.SYSTEM_SUCCESS);
		} catch (Exception e) {
			 log.error("查询电子病历分页失败！",e);
			 result.setStatus(ConstantDefinition.SYSTEM_ERROR);
		}
		 return result;
	}
	
	
  

}
